package nhatquang.gs.controler;

import java.io.IOException;

import nhatquang.gs.main.MainActivity;
import nhatquang.gs.model.UserData;
import nhatquang.gs.model.loadResourcesListener;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public class ResourcesManager {

	public final static ResourcesManager INSTANCE = new ResourcesManager();
	public Engine mEngine;
	public MainActivity mActivity;
	public Camera mCamera;
	public VertexBufferObjectManager VBOM;
	public UserData userData;
	public loadResourcesListener load;

	/*----------------resources of music------------------*/
	public Music mMusic;
	public Sound mAudio;

	/*----------------resources of splash-----------------*/
	public ITextureRegion mTextureRegionSplash;

	/*----------------resources of menu-------------------*/
	public ITextureRegion mTextureRegionMenu;
	public ITextureRegion mTextureRegionMenu_play;
	public ITextureRegion mOnSound;
	public ITextureRegion mOffSound;

	/*-----------------resource of player-----------------*/
	public ITextureRegion mTextureRegionMap_Player;
	public ITextureRegion mTextureRegionPanel_Player;
	public ITextureRegion mTextureRegionBackGround_Player;
	public ITextureRegion mTextureRegionTime_Player;
	public ITextureRegion mTextureRegionPause_Player;
	public ITextureRegion mTextureRegionTimeBlue_Player;
	public ITextureRegion mTextureRegionTimeBrown_Player;
	public ITextureRegion mTextureRegionBug_Player;
	public TiledTextureRegion mTextureRegionScore_Player;
	public TiledTextureRegion mTiledTextureRegionTargetScore_Player;
	public TiledTextureRegion mTiledTextureRegionNumberLevel_Player;
	public ITextureRegion mOnSound_Player;
	public ITextureRegion mOffSound_Player;
	public ITextureRegion mTextureRegionBackground_Player2;
	public ITextureRegion mTextureRegionComplete_Player2;
	public ITextureRegion mTextureRegionMenu_Player2;
	public ITextureRegion mTextureRegionNext_Player2;
	public ITextureRegion mTextureRegionRestart_Player2;
	public ITextureRegion mTextureRegionResume_Player2;
	public ITextureRegion mTextureRegionPause_Player2;

	/*-----------------resource of fruit--------------------*/
	public ITextureRegion[] mTextureRegionFruit;
	public ITextureRegion[] mTextureRegionFruitSell;
	public TiledTextureRegion mTiledTextureRegionBaoza_Fruit;

	/*-----------------resource of level-------------------*/
	public ITextureRegion mTextureRegionOneStar;
	public ITextureRegion mTextureRegionTowStar;
	public ITextureRegion mTextureRegionThreeStar;
	public ITextureRegion mTextureRegionZeroStar;
	public ITextureRegion mTextureRegionClockStar;
	public ITextureRegion mTextureRegionBackGroundLevel;

	public static void prepareManager(Engine mEngine, MainActivity mActivity,
			Camera mCamera, VertexBufferObjectManager VBOM, UserData userData) {
		getInstance().mEngine = mEngine;
		getInstance().mActivity = mActivity;
		getInstance().mCamera = mCamera;
		getInstance().VBOM = VBOM;
		getInstance().userData = userData;

	}

	public static ResourcesManager getInstance() {
		return INSTANCE;
	}

	public void setloadResourcesListener(loadResourcesListener load) {
		this.load = load;
	}

	/*--------------load splash-------------------------*/
	public void loadSplash() {
		mTextureRegionSplash = ResourcesLoader
				.loadResources("logo/logo_company.png");
	}

	/*--------------unload splash-----------------------*/
	public void unloadSplash() {
		mTextureRegionSplash = null;
		System.gc();
	}

	/*------------------load music---------------------*/
	public void loadMusic() {
		// MusicFactory.setAssetBasePath("sound/");
		try {
			mMusic = MusicFactory.createMusicFromAsset(
					mEngine.getMusicManager(), mActivity,
					"sound_background.ogg");
		} catch (IllegalStateException e) {

			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void loadResource() {
		/* -------------------menu-------------------- */
		mTextureRegionMenu = ResourcesLoader
				.loadResources("menu/menu_background.png");
		mTextureRegionMenu_play = ResourcesLoader
				.loadResources("menu/play.png");
		mOnSound = ResourcesLoader.loadResources("menu/sound.png");
		mOffSound = ResourcesLoader.loadResources("menu/soundoff.png");

		/* ---------------player-------------------- */
		mTextureRegionPanel_Player = ResourcesLoader
				.loadResources("player/panel.png");
		mTextureRegionMap_Player = ResourcesLoader
				.loadResources("player/map.png");
		mTextureRegionBackGround_Player = ResourcesLoader
				.loadResources("player/background.png");
		mTextureRegionTime_Player = ResourcesLoader
				.loadResources("player/time.png");
		mTextureRegionPause_Player = ResourcesLoader
				.loadResources("player/pause_audio.png");
		mTextureRegionTimeBlue_Player = ResourcesLoader
				.loadResources("player/timeblue.png");
		mTextureRegionTimeBrown_Player = ResourcesLoader
				.loadResources("player/timebrown.png");
		mTextureRegionBug_Player = ResourcesLoader
				.loadResources("player/bug.png");
		mTextureRegionScore_Player = ResourcesLoader.loadTileResources(
				"player/score.png", 10, 1);
		mTiledTextureRegionTargetScore_Player = ResourcesLoader
				.loadTileResources("player/targetscore.png", 10, 1);
		mTiledTextureRegionNumberLevel_Player = ResourcesLoader
				.loadTileResources("player/numberlevel.png", 10, 1);
		mOnSound_Player = ResourcesLoader.loadResources("player/sound.png");
		mOffSound_Player = ResourcesLoader.loadResources("player/soundoff.png");
		mTextureRegionBackground_Player2 = ResourcesLoader
				.loadResources("player/pausebackground.png");
		mTextureRegionComplete_Player2 = ResourcesLoader
				.loadResources("player/complete.png");
		mTextureRegionMenu_Player2 = ResourcesLoader
				.loadResources("player/menu.png");
		mTextureRegionNext_Player2 = ResourcesLoader
				.loadResources("player/next.png");
		mTextureRegionRestart_Player2 = ResourcesLoader
				.loadResources("player/restart.png");
		mTextureRegionResume_Player2 = ResourcesLoader
				.loadResources("player/resume.png");
		mTextureRegionPause_Player2 = ResourcesLoader
				.loadResources("player/pause.png");

		/*----------------fruit----------------------*/
		mTextureRegionFruit = new ITextureRegion[8];
		mTextureRegionFruitSell = new ITextureRegion[8];

		mTextureRegionFruit[0] = ResourcesLoader
				.loadResources("fruit/fruit1.png");
		mTextureRegionFruit[1] = ResourcesLoader
				.loadResources("fruit/fruit2.png");
		mTextureRegionFruit[2] = ResourcesLoader
				.loadResources("fruit/fruit3.png");
		mTextureRegionFruit[3] = ResourcesLoader
				.loadResources("fruit/fruit4.png");
		mTextureRegionFruit[4] = ResourcesLoader
				.loadResources("fruit/fruit5.png");
		mTextureRegionFruit[5] = ResourcesLoader
				.loadResources("fruit/fruit6.png");
		mTextureRegionFruit[6] = ResourcesLoader
				.loadResources("fruit/fruit7.png");
		mTextureRegionFruit[7] = ResourcesLoader
				.loadResources("fruit/fruit8.png");
		mTextureRegionFruitSell[0] = ResourcesLoader
				.loadResources("fruit/sel01.png");
		mTextureRegionFruitSell[1] = ResourcesLoader
				.loadResources("fruit/sel02.png");
		mTextureRegionFruitSell[2] = ResourcesLoader
				.loadResources("fruit/sel03.png");
		mTextureRegionFruitSell[3] = ResourcesLoader
				.loadResources("fruit/sel04.png");
		mTextureRegionFruitSell[4] = ResourcesLoader
				.loadResources("fruit/sel05.png");
		mTextureRegionFruitSell[5] = ResourcesLoader
				.loadResources("fruit/sel06.png");
		mTextureRegionFruitSell[6] = ResourcesLoader
				.loadResources("fruit/sel07.png");
		mTextureRegionFruitSell[7] = ResourcesLoader
				.loadResources("fruit/sel08.png");
		mTiledTextureRegionBaoza_Fruit = ResourcesLoader.loadTileResources(
				"fruit/baoza1.png", 12, 1);

		/*-----------------level----------------------*/
		mTextureRegionClockStar = ResourcesLoader
				.loadResources("level/clock.png");
		mTextureRegionZeroStar = ResourcesLoader
				.loadResources("level/xing0.png");
		mTextureRegionOneStar = ResourcesLoader
				.loadResources("level/xing1.png");
		mTextureRegionTowStar = ResourcesLoader
				.loadResources("level/xing2.png");
		mTextureRegionThreeStar = ResourcesLoader
				.loadResources("level/xing3.png");
		mTiledTextureRegionNumberLevel_Player = ResourcesLoader
				.loadTileResources("level/levelnumber.png", 10, 1);
		mTextureRegionBackGroundLevel = ResourcesLoader
				.loadResources("level/background.png");

		/* ==================end load -> run application============== */
		load.RunApp();

	}

	public void unloadResource() {
		/* ---------menu----------- */
		mTextureRegionMenu = null;
		mTextureRegionMenu_play = null;

		/* ---------player--------- */
		mTextureRegionPanel_Player = null;
		mTextureRegionMap_Player = null;
		mTextureRegionBackGround_Player = null;
		mTextureRegionTime_Player = null;
		mTextureRegionPause_Player = null;
		mTextureRegionTimeBlue_Player = null;
		mTextureRegionTimeBrown_Player = null;
		mTextureRegionBug_Player = null;
		mTiledTextureRegionTargetScore_Player = null;
		mTiledTextureRegionNumberLevel_Player = null;
		mOnSound_Player = null;
		mOffSound_Player = null;
		mTextureRegionBackground_Player2 = null;
		mTextureRegionComplete_Player2 = null;
		mTextureRegionMenu_Player2 = null;
		mTextureRegionNext_Player2 = null;
		mTextureRegionRestart_Player2 = null;
		mTextureRegionResume_Player2 = null;
		mTextureRegionPause_Player2 = null;

		/*-------------fruit---------------*/
		mTextureRegionFruit[0] = null;
		mTextureRegionFruit[1] = null;
		mTextureRegionFruit[2] = null;
		mTextureRegionFruit[3] = null;
		mTextureRegionFruit[4] = null;
		mTextureRegionFruit[5] = null;
		mTextureRegionFruit[6] = null;
		mTextureRegionFruit[7] = null;
		mTextureRegionFruitSell[0] = null;
		mTextureRegionFruitSell[1] = null;
		mTextureRegionFruitSell[2] = null;
		mTextureRegionFruitSell[3] = null;
		mTextureRegionFruitSell[4] = null;
		mTextureRegionFruitSell[5] = null;
		mTextureRegionFruitSell[6] = null;
		mTextureRegionFruitSell[7] = null;
		mTiledTextureRegionBaoza_Fruit = null;
		mTextureRegionFruit = null;
		mTextureRegionFruitSell = null;

		/*-----------------level----------------------*/
		mTextureRegionClockStar = null;
		mTextureRegionZeroStar = null;
		mTextureRegionOneStar = null;
		mTextureRegionTowStar = null;
		mTextureRegionThreeStar = null;
		mTiledTextureRegionNumberLevel_Player = null;
		mTextureRegionBackGroundLevel = null;

		/* ===========end unload */
		System.gc();
	}
}
